from django import forms
from django.contrib import admin

from amat_app.models import SectionModel, CaseStepModel, CaseModel, ProjectModel, AppiumConfigModel


# Register your models here.
#
@admin.register(AppiumConfigModel)
class AppiumConfigAdmin(admin.ModelAdmin):
    list_display = ('name', 'platformName', 'platformVersion', 'automationName', 'appPackage', 'appActivity')


@admin.register(ProjectModel)
class ProjectAdmin(admin.ModelAdmin):
    list_display = ('name', 'description', 'created_at', 'last_updated_at')


@admin.register(SectionModel)
class SectionAdmin(admin.ModelAdmin):
    list_display = ('name', 'parent')  # 显示模块名称和父模块
    search_fields = ('name',)  # 允许通过模块名称搜索
    list_filter = ('parent',)  # 允许根据父模块过滤


# 小型输入框
class CaseStepModelForm(forms.ModelForm):
    class Meta:
        model = CaseStepModel
        fields = ['order', 'stepName', 'expected', 'parent']
        widgets = {
            'order': forms.TextInput(attrs={'size': '5'}),
            'stepName': forms.TextInput(attrs={'size': '20', 'stype': 'width:100px'}),
            'expected': forms.TextInput(attrs={'size': '20', 'stype': 'width:100px'}),
            'parent': forms.Select(attrs={'style': 'width: 120px;'}),
        }


class CaseStepInline(admin.TabularInline):
    model = CaseStepModel
    extra = 1
    fields = ('order', 'stepName', 'expected', 'parent')
    verbose_name_plural = '步骤'
    form = CaseStepModelForm


class CaseModelForm(forms.ModelForm):
    class Meta:
        model = CaseModel
        fields = ['name', 'description', 'section', 'preCondition', 'priority']
        widgets = {
            'name': forms.TextInput(attrs={'size': '20'}),
            'description': forms.TextInput(attrs={'size': '20', 'style': 'width:100px'}),
            'section': forms.Select(attrs={'style': 'width:120px'}),
            'preCondition': forms.TextInput(attrs={'style': 'width: 120px;'}),
            'priority': forms.TextInput(attrs={'style': 'width: 120px;'}),
        }


class CaseAdmin(admin.ModelAdmin):
    list_display = ('name', 'section', 'priority', 'preCondition', 'created_at', 'updated_at')
    search_fields = ('name',)
    list_filter = ('section', 'priority')
    inlines = [CaseStepInline]
    form = CaseModelForm


admin.site.register(CaseModel, CaseAdmin)
# class StepResultInline(admin.TabularInline):
#     model = StepResultModel
#     extra = 1  # 默认显示的空行数
#
#
# class CaseStepAdmin(admin.ModelAdmin):
#     list_display = ('stepName', 'case', 'order', 'parent')
#     search_fields = ('stepName', 'case__name')  # 支持通过用例名称搜索
#     list_filter = ('case', 'parent')  # 过滤器
#     inlines = [StepResultInline]  # 嵌套步骤结果
#
#
# class StepResultAdmin(admin.ModelAdmin):
#     list_display = ('step', 'status', 'created_at')  # 显示的字段
#     search_fields = ('step__step_name', 'status')  # 支持通过步骤名称和状态搜索
#     list_filter = ('status', )  # 过滤器
#
#
# class CaseStepInline(admin.TabularInline):
#     model = CaseStepModel
#     extra = 1  # 默认显示的空行数
#
#
# class CaseAdmin(admin.ModelAdmin):
#     list_display = ('name', 'section', 'created_at', 'updated_at')  # 假设 CaseModel 有这些字段
#     search_fields = ('name', )  # 支持通过用例名称搜索
#     list_filter = ('section', )  # 过滤器
#     inlines = [CaseStepInline]  # 嵌套步骤
#
#
# class SectionAdmin(admin.ModelAdmin):
#     list_display = ('name', 'created_at', 'updated_at')  # 假设 SectionModel 有这些字段
#     search_fields = ('name', )  # 支持通过名称搜索
